package org.iabako.client.ui.form.extranet.signup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.layout.field.*;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.shared.entity.business.Address;
import org.iabako.shared.entity.enumeration.Country;
import org.iabako.shared.entity.enumeration.LabelEnum;
import org.iabako.shared.entity.enumeration.SignUpStep;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.iservice.user.SignUpServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.Arrays;
import java.util.List;

/**
 * Created by jose on 08/12/14.
 */
public class SignUpEnterpriseForm extends Composite {

    interface MyUiBinder extends UiBinder<HTMLPanel, SignUpEnterpriseForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    protected final SignUpServiceAsync signUpServiceAsync = SignUpServiceAsync.Util.getInstance();

    @UiField
    InputText enterpriseName;
    @UiField
    InputText addressLine1;
    @UiField
    InputText addressLine2;
    @UiField
    InputText city;
    @UiField
    InputText zipCode;
    @UiField
    SelectList country;
    @UiField
    InputText phoneNumber;
    @UiField
    Button goAhead;
    @UiField
    CheckboxBoolean termsAndConditionsCheck;
    @UiField
    LabelText goToTerms;

    SignUpForm signUpForm;

    public SignUpEnterpriseForm() {
        initWidget(binderForm.createAndBindUi(this));
        goAhead.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                validate();
            }
        });

        goToTerms.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                NavigationController.getInstance().closePopups();
                NavigationController.getInstance().goTo(PageEnum.Terms, signUpForm, null);
            }
        });
    }

    @Override
    protected void onLoad() {
        LabelEnum value = country.getEnumValue();
        country.setSorted(true);
        country.setEnumValue(value);
    }

    public void setSignUpForm(SignUpForm signUpForm) {
        this.signUpForm = signUpForm;
    }

    protected List<IField> getMandatoryFields() {
        return Arrays.asList((IField) enterpriseName, addressLine1, city, country, phoneNumber);
    }

    protected Enterprise getEnterprise() {
        Enterprise enterprise = signUpForm.getEnterprise();

        if (enterprise == null) {
            enterprise = new Enterprise();
        }

        enterprise.setName(enterpriseName.getValue());

        Address address = new Address();
        address.setAddressLine1(addressLine1.getValue());
        address.setAddressLine2(addressLine2.getValue());
        address.setCity(city.getValue());
        address.setCountry((Country) country.getEnumValue());
        address.setZipCode(zipCode.getValue());
        enterprise.setAddress(address);

        enterprise.setPhoneNumber(phoneNumber.getValue());

        return enterprise;
    }

    private boolean uiValidation() {

        ValidationMessage.getFirstFromDOM().clear();

        boolean uiValidation = true;
        if (GenericTools.isEmpty(enterpriseName.getValue())) {
            uiValidation = false;
            enterpriseName.addErrorMessage(enterpriseName, false, "validation_form_mandatory_red_fields_error");
        }
        if (GenericTools.isEmpty(addressLine1.getValue())) {
            uiValidation = false;
            addressLine1.addErrorMessage(addressLine1, false, "validation_form_mandatory_red_fields_error");
        }
        if (GenericTools.isEmpty(country.getEnumValue())) {
            uiValidation = false;
            country.addErrorMessage(country, false, "validation_form_mandatory_red_fields_error");
        }
        if (GenericTools.isEmpty(city.getValue())) {
            uiValidation = false;
            city.addErrorMessage(city, false, "validation_form_mandatory_red_fields_error");
        }
        if (GenericTools.isEmpty(phoneNumber.getValue())) {
            uiValidation = false;
            phoneNumber.addErrorMessage(phoneNumber, false, "validation_form_mandatory_red_fields_error");
        }

        if (GenericTools.isEmpty(phoneNumber.getValue())) {
            uiValidation = false;
            phoneNumber.addErrorMessage(phoneNumber, false, "validation_form_mandatory_red_fields_error");
        }

        if (uiValidation && !termsAndConditionsCheck.getValue()) {
            uiValidation = false;
            termsAndConditionsCheck.addErrorMessage(termsAndConditionsCheck, false, "validation_sign_up_no_term_conditions_error");
        }

        return uiValidation;
    }

    public boolean validate() {
        if (!uiValidation()) {
            return false;
        }
        Enterprise enterprise = getEnterprise();
        if (enterprise == null) {
            return false;
        }

        signUpServiceAsync.saveEnterprise(enterprise, new IabakoAsyncCallback<Enterprise>() {
            public void onSuccess(Enterprise result) {
                signUpForm.setEnterprise(result);
                signUpForm.goTo(SignUpStep.signUpUser);
                super.onSuccess(result);
            }
        });
        return false;

    }
}
